package com.block.account.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.account.model.dto.EntryAccountDTO;
import com.block.account.service.IAccountActionHandleService;
import com.block.account.service.IAccountItemSpecialHisService;
import com.block.account.service.IAccountItemSpecialService;
import com.block.common.service.LogFactory;
import com.block.common.util.BigDecimalUtil;
import com.block.db.entity.AccountItemSpecial;
import com.block.db.entity.SpecialCostomInfo;
import com.block.db.mapper.AccountItemSpecialMapper;
import com.block.db.mapper.SpecialCostomInfoMapper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 账户账户信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-07-29
 */
@Service
public class AccountItemSpecialServiceImpl extends ServiceImpl<AccountItemSpecialMapper, AccountItemSpecial> implements IAccountItemSpecialService, IAccountActionHandleService {

    private static Logger log = LogFactory.getCommLog();

    @Resource
    SpecialCostomInfoMapper specialCostomInfoMapper;
    @Autowired
    IAccountItemSpecialHisService accountItemSpecialHisService;

    /**
     * 获取账户信息
     * @param infoId
     * @return
     */
    @Override
    public AccountItemSpecial getAccountInfoByInfoId(String infoId, String accountId) {

        AccountItemSpecial accountItemSpecial = this.baseMapper.getEntityByIdLock(infoId, accountId);
        if (ObjectUtil.isEmpty(accountItemSpecial)){
            accountItemSpecial = this.saveAccountInfo(infoId);
        }
        return accountItemSpecial;
    }

    public AccountItemSpecial saveAccountInfo(String infoId){

        SpecialCostomInfo entryByIdLock = specialCostomInfoMapper.getEntryByIdLock(Long.valueOf(infoId));

        AccountItemSpecial newEntity = new AccountItemSpecial();
        newEntity.setAccountName(entryByIdLock.getName());
        newEntity.setInfoId(entryByIdLock.getId().intValue());
        newEntity.setInfoName(entryByIdLock.getName());
        newEntity.setDeptId(entryByIdLock.getDeptId());
        newEntity.setOwnerId(entryByIdLock.getOwnerUserId());
        newEntity.setOwnerName(entryByIdLock.getOwnerUsername());

        boolean save = this.save(newEntity);

        return this.getById(newEntity.getId());

    }

    /**
     * 同步锁
     * @param entryAccountDTO
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public boolean entryAccountInfo(EntryAccountDTO entryAccountDTO) {

            AccountItemSpecial accountItemSpecial = this.getAccountInfoByInfoId(entryAccountDTO.getInfoId(), entryAccountDTO.getItemAccountId());

            // 修改可用金额
            BigDecimal avaliableAmount = BigDecimalUtil.add(entryAccountDTO.getChangeAmount(), accountItemSpecial.getAvaliableAmount());

            AccountItemSpecial updateEntity = new AccountItemSpecial();
            updateEntity.setId(accountItemSpecial.getId());
            updateEntity.setAvaliableAmount(avaliableAmount);
            boolean updateRes = this.updateById(updateEntity);
            log.info("【机构账户入账】更新机构账户完成【updateRes】{}【entryAccountDTO】{}", updateRes, JSON.toJSONString(entryAccountDTO));
            if (!updateRes){
                return false;
            }

            entryAccountDTO.setItemAccountId(String.valueOf(accountItemSpecial.getId()));

            entryAccountDTO.setOriginAmount(accountItemSpecial.getAvaliableAmount());
            entryAccountDTO.setCurrentAmount(avaliableAmount);
            boolean hisRes = accountItemSpecialHisService.saveAccountHisInfo(accountItemSpecial, entryAccountDTO);
            log.info("【机构账户入账】新增机构账户历史【hisRes】{}【entryAccountDTO】{}", hisRes, JSON.toJSONString(entryAccountDTO));
            if (!hisRes){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
            return hisRes;
    }

}
